package cn.edu.njtc.service.impl;

import cn.edu.njtc.entity.pojo.PatientAttackRecord;
import cn.edu.njtc.entity.vo.PatientAttackRecordVo;
import cn.edu.njtc.mapper.PatientAttackRecordMapper;
import cn.edu.njtc.service.PatientAttackRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PatientAttackRecordServiceImpl extends ServiceImpl<PatientAttackRecordMapper, PatientAttackRecord> implements PatientAttackRecordService {

    @Override
    public List<PatientAttackRecordVo> getRecordsByUserId(Integer userId) {
        LambdaQueryWrapper<PatientAttackRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatientAttackRecord::getUserId, userId)
                .eq(PatientAttackRecord::getDeleted, false);

        return list(queryWrapper).stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
    }

    @Override
    public PatientAttackRecordVo getRecordById(Integer recordId) {
        PatientAttackRecord record = getById(recordId);
        if (record == null || record.getDeleted()) {
            return null;
        }
        return convertToVo(record);
    }

    @Override
    public boolean addRecord(PatientAttackRecord record) {
        return save(record);
    }

    @Override
    public boolean updateRecord(PatientAttackRecord record) {
        return updateById(record);
    }

    @Override
    public boolean deleteRecord(Integer recordId) {
        return removeById(recordId);
    }

    private PatientAttackRecordVo convertToVo(PatientAttackRecord record) {
        PatientAttackRecordVo vo = new PatientAttackRecordVo();
        BeanUtils.copyProperties(record, vo);
        return vo;
    }
}
